package com.hxzy.controller.admin.product.resp;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.hxzy.common.resp.DishFlavorItem;
import com.hxzy.common.resp.DishFlavorItemProp;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author admin
 */
@ApiModel(value = "商品模型")
@Getter
@Setter
public class ProductResp {

    @ApiModelProperty(value = "主键")
    private Long id;


    @ApiModelProperty(value = "商品规格列表")
    private List<DishFlavorItem> dishFlavors;

    /**
     * 商品名称
     */
    @ApiModelProperty(value = "商品名称")
    private String name;

    /**
     * 商品价格
     */
    @ApiModelProperty(value = "商品价格")
    private Double price;

    /**
     * 计算价格明细
     * @return
     */
    @ApiModelProperty(value = "从规格明细中计算商品价格")
    private Double calcPrice;

    /**
     * 重新计算
     * @return
     */
    public Double getCalcPrice() {
        if(this.price>0){
            return this.price;
        }

        AtomicReference<Double> price= new AtomicReference<>(0.0);
        //根据商品明细计算价格
        if(Objects.nonNull(this.dishFlavors) && !this.dishFlavors.isEmpty()){
            //循环遍历各种规格明细 [ {name:xx,  value:[{"value":"中杯","price":14},{"value":"大杯","price":17}]}]，取得明细的数组下面第一个对象的price
            this.dishFlavors.forEach(p->{
                DishFlavorItemProp dishFlavorItemProp = p.getValue().get(0);
                price.updateAndGet(v -> v + dishFlavorItemProp.getPrice());
            });
        }
        return price.get();
    }

    /**
     * 商品图片
     */
    @ApiModelProperty(value = "商品图片")
    private String picture;

    /**
     * 商品描述
     */
    @ApiModelProperty(value = "商品描述")
    private String description;

    /**
     * 商品综合评分
     */
    @ApiModelProperty(value = "商品综合评分")
    private Integer score;

    /**
     * 商品库存（-1不限量）
     */
    @ApiModelProperty(value = "商品库存（-1不限量）")
    private Integer num;

    /**
     * 商家id
     */
    @ApiModelProperty(value = "商家id")
    private Long merchantId;

    @ApiModelProperty(value = "商家名称")
    private String merchantName;

    /**
     * 商品分类
     */
    @ApiModelProperty(value = "商品分类Id")
    private Long categoryId;

    @ApiModelProperty(value = "商品分类名称")
    private String categoryName;

    /**
     * 是否上架 1：上架 ，0：没上架
     */
    @ApiModelProperty(value = "是否上架 1：上架 ，0：没上架")
    private String isgrounding;

    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private Date createTime;




}
